//
//  YPShareAccountInfo.m
//  ChengHuiTong
//
//  Created by KiVen on 15/11/4.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "YPShareAccountInfo.h"

@implementation YPShareAccountInfo

static YPShareAccountInfo * singleton = nil;

+(instancetype)shareAccountInfo{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        if (!singleton)
        {
            singleton = [[YPShareAccountInfo alloc] init];
            
             NSString* path = NSHomeDirectory();
            
            path = [path stringByAppendingPathComponent:@"Library/user.arc"];
            
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                
                YPShareAccountInfo* localUser = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
                
                if ([localUser isKindOfClass:[YPShareAccountInfo class]]) {
                    
                    unsigned int count;
                    Ivar *ivarList = class_copyIvarList([self class], &count);
                    
                    for (int i = 0; i < count; i ++) {
                        //拿到成员变量
                        Ivar ivar = ivarList[i];
                        //生成key
                        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
                        //获取本地value
                        id localValue = [localUser valueForKey:key];
                        //再赋值
                        [singleton setValue:localValue forKey:key];
                    }
                    
                    free(ivarList);
                    
                }
            }

        }
    });
    
    return singleton;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        unsigned int count;
        Ivar *ivarList = class_copyIvarList([self class], &count);
        
        for (int i = 0; i < count; i ++) {
            //拿到成员变量
            Ivar ivar = ivarList[i];
            //生成key
            NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
            //获取value
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        
        free(ivarList);
    }
    
    return self;
    
    

    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    unsigned int count;
    Ivar *ivarList = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i ++) {
        
        //拿到成员变量
        Ivar ivar = ivarList[i];
        //获取key
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        //获取value
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
        
    }
    
    free(ivarList);

}

//个人信息保存到本地
-(void)saveData{
    
    NSString* path = NSHomeDirectory();
    
    
    path = [path stringByAppendingPathComponent:@"Library/user.arc"];
    
    if (singleton) {
        
        [NSKeyedArchiver archiveRootObject:singleton toFile:path];
        
    }
}
//清除个人信息
-(void)clearData{

    self.token = nil;
    self.msg_unread_count = @(0);
    [self saveData];
}

//赋值
-(void)setDataWithDic:(NSDictionary *)dic{
    
    [self setValuesForKeysWithDictionary:dic];
    
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}


@end
